package com.bsb.biz.service;

import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.stereotype.Service;

import com.bsb.base.common.util.StringUtils;

@Service
public class ValidateCodeService {

	private static final Logger logger = Logger.getLogger(ValidateCodeService.class);
	private static int errorCount = 0;

	public boolean checkSmsCode(String code) {
		if (StringUtils.isEmpty(code)) {
			return false;
		}
		Session session = SecurityUtils.getSubject().getSession(false);
		if (null == session) {
			return false;
		}
		String sessionCode = (String) session.getAttribute("smscode");
		if (StringUtils.isEmpty(sessionCode)) {
			return false;
		}
		if (code.equals(sessionCode)) {
			session.removeAttribute("smscode");
			logger.info("验证码正确移除验证码");
			return true;
		} else {
			if (errorCount > 3) {
				session.removeAttribute("smscode");
				logger.info("验证码错误次数超限移除验证码");
				errorCount = 0;
				return false;
			}
			errorCount++;
			return false;
		}
	}
}
